/*******************************************************************************
 * Copyright 2011, 2012 Chris Banes.
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package mamikeji.liaowa.ui.activity.circle;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

import com.bumptech.glide.Glide;

import java.io.File;
import java.util.ArrayList;

import mamikeji.liaowa.R;
import mamikeji.liaowa.ui.activity.BaseActivity;
import mamikeji.liaowa.widget.photoview.HackyViewPager;
import mamikeji.liaowa.widget.photoview.PhotoView;


public class PreviewViewPagerActivity extends BaseActivity {
    private ArrayList<String> imageList;
    private int selectedPosition;
    private ViewPager viewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview_view_pager);
        getBase();
        viewPager = (HackyViewPager) findViewById(R.id.view_pager);
        selectedPosition = getIntent().getIntExtra("position", 0);
        System.out.println("selectedPosition:" + selectedPosition);
        imageList = getIntent().getStringArrayListExtra("imageList");
        setTitleName((selectedPosition + 1) + "/" + imageList.size());
        setTitleLeftImage(R.drawable.icon_fanhuibai_n_fff, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putStringArrayListExtra("imageList",imageList);
                setResult(RESULT_OK,intent);
                finish();
            }
        });
        setTitleRightImage(R.drawable.icon_shanchuzhuangtai_n, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imageList.remove(selectedPosition);
                if (imageList != null && imageList.size() > 0) {
                    viewPager.setAdapter(new SamplePagerAdapter(imageList));
                    if (selectedPosition == imageList.size()) {
                        selectedPosition = selectedPosition - 1;
                    } else if (selectedPosition == 0) {
                        selectedPosition = 0;
                    }
                    viewPager.setCurrentItem(selectedPosition);
                    setTitleName((selectedPosition + 1) + "/" + imageList.size());
                } else {
                    Intent intent = new Intent();
                    intent.putStringArrayListExtra("imageList",new ArrayList<String>(0));
                    setResult(RESULT_OK,intent);
                    finish();
                }

            }
        });


        viewPager.setAdapter(new SamplePagerAdapter(imageList));
        viewPager.setCurrentItem(selectedPosition);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectedPosition = position;
                setTitleName((selectedPosition + 1) + "/" + imageList.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    class SamplePagerAdapter extends PagerAdapter {
        private ArrayList<String> imageList;

        public SamplePagerAdapter(ArrayList<String> imageList) {
            this.imageList = imageList;
        }

        @Override
        public int getCount() {
            return imageList.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            PhotoView photoView = new PhotoView(container.getContext());
            Glide.with(PreviewViewPagerActivity.this).load(new File(imageList.get(position))).into(photoView);
            container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            System.out.println("返回键");
            Intent intent = new Intent();
            intent.putStringArrayListExtra("imageList",imageList);
            setResult(RESULT_OK,intent);
            finish();
            return false;
        }else{
            return super.onKeyDown(keyCode, event);
        }
    }

}
